package com.lhf.fvsgateway.config;

import com.alibaba.fastjson.JSONObject;
import com.lhf.common.enums.Status;
import com.lhf.common.util.Result;
import com.lhf.fvsgateway.config.handler.AccessDeniedHandler;
import com.lhf.fvsgateway.config.handler.AuthenticationEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverterAdapter;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/4/1 11:18
 * @since 1.0
 */
@Configuration
@EnableWebFluxSecurity
public class ResourceConfig {

    @Resource(name = "accessDeniedHandler")
    private AccessDeniedHandler accessDeniedHandler;
    @Resource(name = "authenticationEntryPoint")
    private AuthenticationEntryPoint authenticationEntryPoint;
    @Resource(name="reactiveAuthorizationManager")
    private ReactiveAuthorizationManager reactiveAuthorizationManager;

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity security) {
        security.exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler)//未授权
                .authenticationEntryPoint(authenticationEntryPoint)//未认证
                .and()
                .authorizeExchange()
                .anyExchange().access(reactiveAuthorizationManager)
//                .anyExchange().authenticated()
                .and()
                .oauth2ResourceServer()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler)
                .jwt()
                .jwtAuthenticationConverter(jwtAuthenticationConverter());
        return security.build();
    }

    @Bean
    public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() {
        JwtGrantedAuthoritiesConverter converter = new JwtGrantedAuthoritiesConverter();
        converter.setAuthorityPrefix("ROLE_");
        converter.setAuthoritiesClaimName("authorities");
        JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(converter);
//        ReactiveJwtAuthenticationConverter authenticationConverter = new ReactiveJwtAuthenticationConverter();
//        authenticationConverter.setJwtGrantedAuthoritiesConverter();
        return new ReactiveJwtAuthenticationConverterAdapter(jwtAuthenticationConverter);
    }


}
